<?php

namespace app\api\controller;
/**
 * ============================================================================
 * DSKMS多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 浏览历史控制器
 */
class  Membergoodsbrowse extends MobileMember {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 日期显示我的足迹
     */
    public function memberbrowse_list() {
        $goodsbrowse_model = model('goodsbrowse');
		$page = intval(input('param.page'));
		$per_page = intval(input('param.per_page'));
        //查询浏览记录
        $condition = array();
        $condition[] = array('member_id','=',$this->member_info['member_id']);
		$timearray = $goodsbrowse_model->getGoodsbrowseList($condition,'goodsbrowse_time',0,'goodsbrowse_time desc');
		foreach($timearray as $key => $val){
			$timearray[$key]['goodsbrowse_time'] = date('Y-m-d',$val['goodsbrowse_time']);
		}
		
		$timearray = array_unique(array_column($timearray,'goodsbrowse_time'));//去除日期数组里面的重复日期
		$count = count($timearray);//日期数组长度
		$page_total = ceil($count/$per_page);//总页数
		if(($count - ($per_page * $page)) > 0){
			$hasmore = true;
		}else{
			$hasmore = false;
		}
		
		$start = $per_page * ($page - 1);//数组起始位置
		$pagetimearray = array_slice($timearray,$start,$per_page);//获取页数日期数组
		$browsegoodslist = array();
		foreach($pagetimearray as $key => $val){
			$browsegoodslist[$key]['time'] =$val;
			$condition   = array();
			$condition[] = array('member_id' ,'=', $this->member_info['member_id']);
			$condition[] = array('goodsbrowse_time' ,'>=', strtotime($val));
			$condition[] = array('goodsbrowse_time' ,'<=', strtotime($val)+86399);
			$goodsidarray = $goodsbrowse_model->getGoodsbrowseList($condition,'goods_id');
			$goodsidarray = array_column($goodsidarray,'goods_id');
			$goodsidarray = implode(',',$goodsidarray);
			$goods_list = model('goods')->getGoodsList(array(array('goods_id','in', $goodsidarray)), 'goods_id, goods_name, goods_advword, goods_promotion_price,goods_price, goods_promotion_type, goods_image, store_id, gc_id, gc_id_1, gc_id_2, gc_id_3');
			foreach($goods_list as $k => $v){
				$goods_list[$k]['goods_image_url'] = goods_cthumb($v['goods_image'], 1260, $v['store_id']);
			}
			$browsegoodslist[$key]['goods_list'] =$goods_list;
		}
        $result = array_merge(array('goodsbrowse_list' => $browsegoodslist),array('hasmore' => $hasmore),array('page_total' => $page_total));
        ds_json_encode(10000, '', $result);
    }
	
	/**
	     * 我的足迹列表
	     */
	    public function browse_list() {
	        $goodsbrowse_model = model('goodsbrowse');
	        //查询浏览记录
	        $condition = array();
	        $condition[] = array('member_id','=',$this->member_info['member_id']);
	        $browselist = $goodsbrowse_model->getViewedGoodsList($this->member_info['member_id'], $this->pagesize);
	        $goodsid_arr = array();
	        foreach ((array) $browselist as $k => $v) {
	            $goodsid_arr[] = $v['goods_id'];
	        }
	        //查询商品信息
	        $browselist_new = array();
	        if ($goodsid_arr) {
	            $goods_list_tmp = model('goods')->getGoodsList(array(array('goods_id','in', $goodsid_arr)), 'goods_id,goods_price,goods_salenum, goods_name, goods_image, store_id, gc_id, gc_id_1, gc_id_2, gc_id_3');
	            $goods_list = array();
	            foreach ((array) $goods_list_tmp as $v) {
	                $goods_list[$v['goods_id']] = $v;
	            }
	            foreach ($browselist as $k => $v) {
	                if ($goods_list[$v['goods_id']]) {
	                    $tmp = array();
	                    $tmp = $goods_list[$v['goods_id']];
	                    $tmp["goodsbrowse_time"] = $v['goodsbrowse_time'];
	                    $tmp["goods_image_url"] = goods_cthumb($goods_list[$v['goods_id']]['goods_image'], 270, $goods_list[$v['goods_id']]['store_id']);
	                    if (date('Y-m-d', $v['goodsbrowse_time']) == date('Y-m-d', TIMESTAMP)) {
	                        $tmp['browsetime_day'] = '今天';
	                    } elseif (date('Y-m-d', $v['goodsbrowse_time']) == date('Y-m-d', (TIMESTAMP - 86400))) {
	                        $tmp['browsetime_day'] = '昨天';
	                    } else {
	                        $tmp['browsetime_day'] = date('Y年m月d日', $v['goodsbrowse_time']);
	                    }
	                    $tmp['browsetime_text'] = $tmp['browsetime_day'] . date('H:i', $v['goodsbrowse_time']);
	                    $browselist_new[] = $tmp;
	                }
	            }
	        }
	        $result = array_merge(array('goodsbrowse_list' => $browselist_new), mobile_page($goodsbrowse_model->page_info));
	        ds_json_encode(10000, '',$result);
	    }

    /**
     * 清空足迹
     */
    public function browse_clearall() {
        //清除缓存中浏览记录
        dcache($this->member_info['member_id'], 'goodsbrowse');
        model('goodsbrowse')->delGoodsbrowse(array('member_id' => $this->member_info['member_id']));
        ds_json_encode(10000, '',1);
    }

}